var app = getApp();
var uploadjs=require('../../utils/upload.js');
var util=require('../../utils/util.js')
Page({
  data: {
    hidden: false,
    defaultimg: '../../img/pic.png',
    imgPath: '../../img/pic.png',
    imgPath2: '',
    imgPath3: '',
    imgLen: 0,
    temp: [] ,
    stars:[1,1,1,1,0],
    willupload:[],
    inupload:false,
    succupload:[], 
    evaluate:{}
  },
  onLoad:function(options){
    this.setData({
      'evaluate.id': options.oid,
      'evaluate.name': options.name
    })
    uploadjs.initupload();
  },
  choose: function() {
    this.setData({
      hidden: !this.data.choose
    })
  },
  
  choosePic: function () {
    console.log(this.data.temp)
    var that = this;
    if (this.data.imgPath == that.data.defaultimg) {
      wx.chooseImage({
        count: 3,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          var imgpath=res.tempFilePaths[0];
          var temp = [that.data.imgPath2, that.data.imgPath3, that.data.imgPath];
          if (temp[0]==''){
            temp[0] = imgpath;
          } else if (temp[1]==''){
            temp[1] = imgpath;
          } else if (temp[2] == '' || temp[2] == that.data.defaultimg){
            temp[2] = imgpath;
          }
          that.setData({
            temp: temp,
            imgPath2: temp[0],
            imgPath3: temp[1],
            imgPath: temp[2],
          })
          uploadjs.upload(imgpath,that.uploadFinished);
        }
      })
    }
  },
  
  uploadFinished:function(data){
    this.setData({
      succupload:data
    })
  },

  toSubmit: function () {
    if (uploadjs.isfinished){
      var that=this;
      var evaluate=this.data.evaluate;
      evaluate.pic = this.data.succupload;
      evaluate.hidden=this.data.hidden;
      var star=0;
      for (var i=0;i<this.data.stars.length;i++){
        star += this.data.stars[i];
      }
      evaluate.star=star;
      var url = util.url.user_evaluate_add;
      util.fetchPost(
        url,
        evaluate,
        function(data){
          wx.redirectTo({
            url: '../orderDetail/orderDetail?oid=' + that.data.evaluate.id,
          })
        }
      )

    }else{
      util.showinfo("图片上传中，请稍后");
    }
  },
  del: function(e) {
    var defaultimg =this.data.defaultimg;
    var i = e.currentTarget.dataset.id;
    if(i==0){
      if (this.data.imgPath != defaultimg) {
        this.setData({
          imgPath2: this.data.imgPath3,
          imgPath3: this.data.imgPath,
          imgPath: defaultimg,
          imgLen: this.data.imgLen - 1
        })
      }else {
        this.setData({
          imgPath2: this.data.imgPath3,
          imgPath3: '',
          // imgPath2: '',
          imgLen: this.data.imgLen - 1
        })
      }      
    }else if(i==1) {
      if (this.data.imgPath != defaultimg) {
        this.setData({
          imgPath3: this.data.imgPath,
          imgPath: defaultimg,
          imgLen: this.data.imgLen - 1
        })
      } else {
        this.setData({
          imgPath3: '',
          imgLen: this.data.imgLen - 1
        })
      }   
    }else if(i==2) {
      this.setData({
        imgPath: defaultimg,
        imgLen: this.data.imgLen - 1
      })
    }
  },
  setStar:function(e){
    var index=e.currentTarget.dataset.index;
    var stars = this.data.stars;
    var star=[];
    for (var i=0;i<stars.length;i++){
      if (i<=index){
        star.push(1);
      }else{
        star.push(0);
      }
    }
    this.setData({ 
      stars:star,
      'evaluate.star':index+1
      });

  },
  setEvaluateText:function(e){
    var value=e.detail.value;
    this.setData({
      'evaluate.content':value,
    })
  }
})